package aom.ju.ss.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.res.Resources;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.WindowManager;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import aom.ju.ss.R;


public abstract class BaseDialog {

    protected Dialog dialog;

    protected ViewDataBinding vBinding;

    private int width = 300;//测试提交 -1 全屏MATCH_PARENT  layoutParams. -2 包裹自身 WRAP_CONTENT

    private int High = -2;//测试提交 -1 全屏MATCH_PARENT  layoutParams. -2 包裹自身 WRAP_CONTENT

    protected Activity activity;

    public int getHigh() {
        return High;
    }

    public void setWidth(int width) {
        this.width = width;
        initWhite();
    }

    public void setHigh(int High) {
        this.High = High;
        initWhite();
    }


    private void initWhite() {
        if (dialog == null || dialog.getWindow() == null) {
            return;
        }
        WindowManager.LayoutParams layoutParams = dialog.getWindow().getAttributes();// ViewGroup.LayoutParams.MATCH_PARENT ViewGroup.LayoutParams.WRAP_CONTENT
        layoutParams.width = width == -1 ? -1 : width == -2 ? -2 : dip2px(width);// -1 全屏MATCH_PARENT  layoutParams. -2 包裹自身 WRAP_CONTENT
        layoutParams.height = High == -1 ? -1 : High == -2 ? -2 : dip2px(High);
        dialog.getWindow().setAttributes(layoutParams);


    }

    public BaseDialog(Activity activity, Object... o) {
        this.activity = activity;
        initView();
        init(o);
        init();
    }
    public void setHeight(int height) {
        this.High = height;
        initWhite();
    }

    public BaseDialog(Activity activity) {
        this.activity = activity;
        initView();

        init();

    }

    public void dis() {
        try {
            dialog.dismiss();
        } catch (Exception ignored) {
        }

    }


    public boolean isBottom() {
        return false;
    }

    protected void initView() {

        dialog = new Dialog(activity, isBottom() ? R.style.dialogBoom : R.style.dialog);

        vBinding = DataBindingUtil.inflate(LayoutInflater.from(activity), getLay(), null, false);

        dialog.getWindow().setGravity(isBottom()? Gravity.BOTTOM: Gravity.CENTER);

        dialog.setContentView(vBinding.getRoot());

        initWhite();

//        {
//            @Override
//            protected void onCreate(Bundle savedInstanceState) {
//                super.onCreate(savedInstanceState);
//                vBinding = DataBindingUtil.inflate(LayoutInflater.from(activity), getLay(), null, false);
//                setContentView(vBinding.getRoot());
//                initWhite();
//            }
//        };

        dialog.show();
    }
    private   int dip2px(int dpValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    protected   void init(){

    };

    public void init(Object... o) {

    }


    protected abstract int getLay();


}
